using UnityEngine;

public class LogData
{
    public int orderId;
    public string content;
}

public class LogDataModel : IDataModel<LogDataModel,LogData,int>
{
    public int orderId;
    public string content;

    public int Key => orderId;

    public bool Equals(LogData p)
    {
        return orderId == p.orderId;
    }

    public bool Equals(int l, int r)
    {
        return l == r;
    }

    public int KeyOf(LogData p)
    {
        return orderId;
    }

    public bool LessThan(LogDataModel other)
    {
        return orderId < other.orderId;
    }

    public void Reset()
    {
        orderId = 0;
        content = string.Empty;
    }

    public void Set(LogData data)
    {
        orderId = data.orderId;
        content = data.content;
    }
}

public class LogDataManager : DataModelSingleton<LogDataManager, LogDataModel, LogData, int>
{
    public override int ID => CtrlEventID.LOG_MANAGER_ID;

    protected override System.Func<IDataModelCenter<LogDataModel, LogData, int>> DataModelCreator => AsList(() => new LogDataModel(),ID);

    int logId;
    protected override void OnAwake()
    {
        logId = 0;
    }

    void OnEnable()
    {
        Application.logMessageReceived += LogCallback;
    }

    void OnDisable()
    {
        Application.logMessageReceived -= LogCallback;
    }

    void LogCallback(string condition, string stackTrace, LogType type)
    {
        var content = $"[{type}]:[{condition}]";
        Add(new LogData
        {
            content = content,
            orderId = logId++,
        });
    }
}